package com.nerve.android.cloudoffice.app.activity.department;

import java.util.Calendar;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;
import org.nerve.android.ui.dialog.DialogHelper.ConfirmListener;
import org.nerve.android.ui.dialog.SelectDialog;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.common.Resource;
import com.nerve.android.cloudoffice.domain.Department;
import com.nerve.android.cloudoffice.domain.User;
import com.nerve.android.cloudoffice.service.data.DepartmentData;

/**
 * 
 * @project Cloudoffice AddUserActivity.java
 * @datetime Aug 12, 2013 4:22:04 PM
 * @description 
 *    
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_member_add)
public class AddUserActivity extends NavigationActivity implements OnClickListener{
	
	public static final int DATE_DIALOG = 100001;
	
	private User user = new User();
	private int departmentId;
	private int position = 0;
	
	@ViewOnId(id=R.id.name)
	private EditText nameET;
	@ViewOnId(id=R.id.password)
	private EditText passwordET;
	@ViewOnId(id=R.id.deparment_id)
	private EditText departmentET;
	@ViewOnId(id=R.id.position, clickListener="this")
	private EditText positionET;
	@ViewOnId(id=R.id.phone)
	public EditText phoneET;
	@ViewOnId(id=R.id.sexGroup)
	private RadioGroup sexGroup;
	@ViewOnId(id=R.id.address)
	public EditText addressET;
	@ViewOnId(id=R.id.birthday, clickListener="this")
	private EditText birthdayET;
	
	private SelectDialog timeSelectDialog;
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg){
			hideProgressDialog();
			switch(msg.what){
			case WORKER_OK:
				toast("保存成功");
				onNavigateBackButtonClick();
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("新增职员");
		customNavigateRightButton("保存", true);
		
		positionET.setText(Resource.User.POSITION[position]);
		
		Department d = (Department)getData(Const.DEPARTMENT, true);
		if(d != null){
			departmentET.setText(d.getName());
			departmentId = d.getId();
		}
	}
	
	@Override
	protected void initData() {
	}
	
	/**
	 * 保存按钮被点击时
	 */
	@Override
	protected void onNavigateRightButtonClick() {
		if(isEmpty(nameET)){
			toast("姓名不能为空");
			return ;
		}
			
		if(isEmpty(passwordET)){
			toast("密码不能为空");
			return ;
		}
		
		user = new User();
		user.setName(nameET.getText().toString());
		user.setPassword(passwordET.getText().toString());
		user.setPosition(position);
		user.setCellPhone(getText(phoneET));
		user.setSex(sexGroup.getCheckedRadioButtonId() == R.id.radio_man? User.MALE:User.FEMALE);
		user.setAddress(getText(addressET));
		user.setBirthday(getText(birthdayET));
		
		new UserWorker().execute();
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.back(2);
	}
	

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.position:
			selectPosition();
			break;
		case R.id.birthday:
			showDialog(DATE_DIALOG);
			break;
		}
	}
	
	/**
	 * @method name: selectPosition
	 * @description: 选择时间
	 * @return type: void
	 *
	 */
	private void selectPosition(){
		if(timeSelectDialog == null){
			timeSelectDialog = new SelectDialog(this, R.layout.dialog_select_position);
			timeSelectDialog.setTag("select");
			timeSelectDialog.setIcon(R.drawable.ico_ok);
			
			timeSelectDialog.setConfirmListener(new ConfirmListener() {
				
				@Override
				public void onConfirmClick(int p, Object obj) {
					position = p;
					positionET.setText(Resource.User.POSITION[position]);
				}
			});
		}
		
		timeSelectDialog.setCurSelect(position);
		timeSelectDialog.show();
	}
	
	/**
     * 创建日期选择对话框
     */
    @Override
    protected Dialog onCreateDialog(int id) {
    	Dialog dialog = null;
		switch (id) {
		case DATE_DIALOG:
			Calendar calendar = Calendar.getInstance();
			dialog = new DatePickerDialog(
					this,
					new DatePickerDialog.OnDateSetListener() {
						public void onDateSet(DatePicker dp, int year,
								int month, int dayOfMonth) {
							
							//设置日期
							birthdayET.setText( year + "-" + (month + 1) + "-"
									+ dayOfMonth);
						}
					}, 
					calendar.get(Calendar.YEAR), // 传入年份
					calendar.get(Calendar.MONTH), // 传入月份
					calendar.get(Calendar.DAY_OF_MONTH) // 传入天数
			);
			dialog.setTitle("选择日期");
			break;
		}
		return dialog;
    }
	
	/**
	 * @project: Cloudoffice
	 * @file: AddUserActivity.java
	 * @package: com.nerve.android.cloudoffice.app.activity.department
	 * @description:
	 *
	 * @author: 集成显卡	1053214511@qq.com
					IBM GDC, zengxm@cn.ibm.com
	 * @date&time: Aug 15, 2013
	 * @change log:
	 */
	private class UserWorker extends AsyncTask<Void, Void, Integer>{

		@Override
		protected Integer doInBackground(Void... params) {
		
			Reply re = DepartmentData.addUser(departmentId, user);
			if(re.isError())
				Const.ERROR = re.getMessage().toString();
			
			return re.isError()? WORKER_FAIL: WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}

	}

}